import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

import java.util.Set;

public class RedisGeneralDemo {

    public static void test1(Jedis jedis) {
        // get set
        System.out.println("get set");

        SetParams params = new SetParams();
//        params.xx();
        params.nx();
        params.ex(10);

        jedis.set("key1", "111", params);

        String key1 = jedis.get("key1");
        System.out.println("key1 = " + key1);
    }

    private static void test2(Jedis jedis) {
        System.out.println("exist del");

        jedis.set("key1", "111");
        jedis.set("key2", "222");
        jedis.set("key3", "333");

        boolean exist = jedis.exists("key1");
        System.out.println("exist = " + exist);
        long result = jedis.del("key1");
        System.out.println("result = " + result);
        exist = jedis.exists("key1");
        System.out.println("exist = " + exist);
        result = jedis.del("key1", "key2", "key3");
        System.out.println("result = " + result);

    }

    private static void test3(Jedis jedis) {
        System.out.println("keys");

        jedis.set("key1", "111");
        jedis.set("ky", "111");
        jedis.set("key3", "111");
        jedis.set("key", "111");

        Set<String> keys = jedis.keys("*");
        System.out.println(keys);
        keys = jedis.keys("k?y");
        System.out.println(keys);
        keys = jedis.keys("*\\d{1}");
        System.out.println(keys);

    }

    private static void test4(Jedis jedis) throws InterruptedException {
        System.out.println("ttl expire");

        jedis.set("key", "111");
        long ttl = jedis.ttl("key");
        System.out.println(ttl);

        SetParams params = new SetParams();
        params.ex(5);

        jedis.set("key", "111", params);
        ttl = jedis.ttl("key");
        System.out.println(ttl);

        Thread.sleep(3000);
        ttl = jedis.ttl("key");
        System.out.println(ttl);

        Thread.sleep(3000);
        ttl = jedis.ttl("key");
        System.out.println(ttl);
    }

    private static void print5(Jedis jedis, String key) {
        String type = jedis.type(key);
        System.out.println(type);
    }

    private static void test5(Jedis jedis) {
        System.out.println("type");

        jedis.set("key1", "111");
        print5(jedis, "key1");

        jedis.hset("k2", "f1", "111");
        print5(jedis, "k2");

        jedis.rpush("k3", "1", "2", "3");
        print5(jedis, "k3");

        jedis.sadd("k4", "1", "2", "2");
        print5(jedis, "k4");

        jedis.zadd("k5", 10, "lisi");
        print5(jedis, "k5");
    }

    public static void main(String[] args) throws Exception {
        // 这里 url 是端口转发配置后的结果
        // 配置成功需要注意两点：
        // 1. 端口转发
        // 2. 云服务器中的redis配置ip（bind）是0.0.0.0 并且保护模式（protected-mode）关闭
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()) {
//            String pong = jedis.ping();
//            System.out.println(pong);
            jedis.flushAll();
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
            test5(jedis);
        }
    }
}
